package com.zzwc.vote.www.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zzwc.vote.exception.ZhiZhiException;
import com.zzwc.vote.utils.AddressUtils;
import com.zzwc.vote.www.entity.VoteEnterprise;
import com.zzwc.vote.www.entity.VoteTerritory;
import com.zzwc.vote.www.entity.VoteUser;
import com.zzwc.vote.www.mapper.VoteEnterpriseMapper;
import com.zzwc.vote.www.mapper.VoteTerritoryMapper;
import com.zzwc.vote.www.service.IVoteEnterpriseService;
import com.zzwc.vote.www.service.IVoteTerritoryService;
import com.zzwc.vote.www.service.IVoteUserService;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author weirdor
 * @since 2018-06-06
 */
@Service
public class VoteTerritoryServiceImpl extends ServiceImpl<VoteTerritoryMapper, VoteTerritory> implements IVoteTerritoryService {

    @Autowired
    private IVoteUserService voteUserService;

    @Autowired
    private IVoteEnterpriseService voteEnterpriseService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void castVote(String openId, String companyId, String ip) {
        EntityWrapper<VoteUser> userEntityWrapper = new EntityWrapper<>();

        userEntityWrapper.eq("open_id", openId);

        VoteUser user = voteUserService.selectOne(userEntityWrapper);

        VoteEnterprise company = voteEnterpriseService.selectById(companyId);

        AddressUtils addressUtils = new AddressUtils();
        String address = "";
        try {
            address = addressUtils.getAddresses("ip=" + ip, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (user != null && company != null) {

            //判断当前用户是否在当天已经投票
            EntityWrapper<VoteTerritory> voteTerritoryEntityWrapper = new EntityWrapper<>();

            voteTerritoryEntityWrapper.eq("user_id", user.getId()).and().gt("vote_time", DateUtil.beginOfDay(new Date())).and().le("vote_time", DateUtil.endOfDay(new Date()));

            VoteTerritory territory = super.selectOne(voteTerritoryEntityWrapper);


            if (territory == null) {
                VoteTerritory voteTerritory = new VoteTerritory();
                voteTerritory.setCreateTime(new Date());
                voteTerritory.setEnterpriseId(companyId);
                voteTerritory.setIp(ip);
                voteTerritory.setLocation(address);
                voteTerritory.setVoteTime(new Date());
                voteTerritory.setUserId(user.getId());
                boolean flag = super.insert(voteTerritory);
                if (flag) {
                    int count = company.getCount();
                    company.setCount(inc(count));
                    voteEnterpriseService.updateById(company);
                }
            } else {
                throw new ZhiZhiException("今天已经投票", 200);
            }

        } else {
            throw new ZhiZhiException("找不到当前用户", 404);
        }

    }

    public synchronized int inc(int count) {
        return ++count;
    }
}
